﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cisoft.Booking.Model;
using Cisoft.Booking.BLL;

public partial class Business_Customer_CustomerEdit : WebUI.Page.BasePage
{
    private int CustomerID = 0;
    private CustomersBO cBO = new CustomersBO();
    private CustomersMO cMO = new CustomersMO();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            CustomerID = GetCustomerID();
            FillModel(CustomerID);
        }
    }

    #region 获得传入的标识 int GetRuleID()
    /// <summary>
    /// 获得传入的标识
    /// </summary>
    private int GetCustomerID()
    {
        int id = 0;
        if (Request.QueryString["CustomerID"] != null)
        {
            id = int.Parse(Request.QueryString["CustomerID"].ToString());
        }

        return id;
    }
    #endregion

    #region 数据绑定控件 void FillModel(int CustomerID)
    /// <summary>
    /// 数据绑定控件
    /// </summary>
    /// <param name="CustomerID">员工标识</param>
    private void FillModel(int CustomerID)
    {
        cMO = cBO.GetModel(CustomerID);
        if (cMO != null)
        {
            tb_CustomerName.Text = cMO.CustomerName;
            tb_CustomerMobile.Text = cMO.CustomerMobile;
        }
    }
    #endregion

    #region 修改事件
    protected void btn_Edit_Click(object sender, ImageClickEventArgs e)
    {
        int result = 0;
        string strError = "";

        string cname = tb_CustomerName.Text;
        string cmobile = tb_CustomerMobile.Text;

        cMO.CustomerName = cname;
        cMO.CustomerMobile = cmobile;

        if (cBO.UpdateIsExsistCustomerName(cMO.CustomerName,cname))
        {
            PageError("该员工名称已经存在！", Request.RawUrl);
        }
        else
        {
            cMO.CustomerName = cname;
        }
        if (cBO.UpdateIsExsistCustomerMobile(cMO.CustomerMobile,cmobile))
        {
            PageError("该员工手机号码已经存在！", Request.RawUrl);
        }
        else
        {
            cMO.CustomerMobile = cmobile;
        }
        if (!Cisoft.IntegrationSystem.Common.Function.IsMobile(cmobile))
        {
            PageError("请确认该手机是天翼手机！", Request.RawUrl);
        }

        try
        {
            result = cBO.Update(cMO);
        }
        catch(Exception ex)
        {
            strError = ex.Message;
        }
        if (result > 0)
        {
            PageRight("修改员工成功！", "CustomerManage.aspx", "返回", "");
        }
        else
        {
            PageError("修改失败，错误如下：" + strError, "CustomerManage.aspx");
        }
    }
    #endregion

    #region 返回事件
    protected void btn_Back_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("CustomerManage.aspx");
    }
    #endregion
}
